<?php
class m_sppd_perjalanan_model extends CI_Model{ 

	function m_sppd_perjalanan_model()
	{
		parent::__construct();
	}
	function getListKota($limit='',$offset=''){
			$menus='';
			$judul=$this->input->post('judul');
			$status=$this->session->userdata('STATUS');
			$query=$this->db->query("select  *,(select nama_kota from m_kota where id=a.dari) as kotasal,(select nama_kota from m_kota where id=a.tujuan) as kotatujuan ,a.id as sppdid ,m_kendaraan.nama_kendaraan 
			from m_sppd_kota a
			LEFT JOIN m_kendaraan ON m_kendaraan.id=a.id_kendaraan
   		 	ORDER BY a.id DESC");
			 if ($query->num_rows() > 0) {
				foreach ($query->result() as $data) {
					$menus[]=$data;
				}
				return $menus;
			}

		}
 
	function count_kota($id=''){
		$jumlah='';
		$status=$this->session->userdata('STATUS');
		$query=$this->db->query("select count(1) as jumlah from m_sppd_kota");
			 if ($query->num_rows() > 0) {
				foreach ($query->result() as $data) {
				$jumlah=$data->jumlah;
				}
				return $jumlah;
			}
	}
	 
	function getdataperjalanan($id=''){
			$query=$this->db->query("select * from m_sppd_kota 
			where id='$id'");
			return $query->row();
	}
	function simpan($id=''){
		$comboAsal=$this->input->post('comboAsal');
		$comboTujuan=$this->input->post('comboTujuan');
		$comboKendaraan=$this->input->post('comboKendaraan');
		$totalharga=$this->input->post('totalharga');
		
		$data=array(
	 	 'dari'=>$comboAsal,
		 'tujuan'=>$comboTujuan,
		 'id_kendaraan'=>$comboKendaraan,
		 'nominal'=>$totalharga,
		);
		if($id!='0'){
		$this->db->query("update m_sppd_kota set dari='$comboAsal',tujuan='$comboTujuan',id_kendaraan='$comboKendaraan',nominal='$totalharga' where id='$id'");	 } else {
		$this->db->trans_start();
		$this->db->insert('m_sppd_kota',$data);
		$this->db->trans_complete(); 
		}
		
	}	
   
	function delete($id=''){
		$this->db->delete('artikel', array('id' => $id)); 
		echo"Sukses Hapus Data...";
	}
	 
	function generateComboAsal($id=''){	
	$table="<select id='comboAsal' onChange='return cariTotalBiaya()' name='comboAsal'><option value=''>-Pilih Kota Asal-</option>";	
	$q2=$this->db->query("select * from m_kota");
			if ($q2->num_rows() > 0) {
			foreach ($q2->result() as $row) {
				$selected="";
				if($id==$row->id){
					$selected="selected='selected'";
				}
				$table.="<option ".$selected." value='".$row->id."'>".$row->nama_kota."</option>"; 
			}
		}
	$table.="</select>";
		return  $table;	
	}
	function generateComboTujuan($id=''){
		$table="<select id='comboTujuan' onChange='return cariTotalBiaya()' name='comboTujuan'><option value=''>-Pilih Kota Tujuan-</option>";	
		$q2=$this->db->query("select * from m_kota");
			if ($q2->num_rows() > 0) {
			foreach ($q2->result() as $row) {
				$selected="";
				if($id==$row->id){
					$selected="selected='selected'";
				}
				$table.="<option ".$selected."  value='".$row->id."'>".$row->nama_kota."</option>"; 
			}
		}
		$table.="</select>";
		return  $table;	
	}
	function generateComboKendaraan($id=''){
		$table="<select id='comboKendaraan' onChange='return cariTotalBiaya()' name='comboKendaraan'><option value=''>-Pilih Jenis Kendaraan-</option>";	
		$q2=$this->db->query("select *from m_kendaraan");
			if ($q2->num_rows() > 0) {
			foreach ($q2->result() as $row) {
				$selected="";
				if($id==$row->id){
					$selected="selected='selected'";
				}
				$table.="<option ".$selected."  value='".$row->id."'>".$row->nama_kendaraan."</option>"; 
			}
		}
		$table.="</select>";
		return  $table;	
	}
	
	function cariTotalBiaya(){
		$dari=$this->input->post('comboAsal');
		$tujuan=$this->input->post('comboTujuan');
		$kendaraan=$this->input->post('comboKendaraan');
		$nominal=0;
		$q2=$this->db->query("select * from m_sppd_kota  where dari='$dari' and tujuan='$tujuan' and id_kendaraan='$kendaraan' ");
				if ($q2->num_rows() > 0) {
				foreach ($q2->result() as $row) {
					 $nominal=$row->nominal;
				}
			}	 
			echo  'Rp.'.number_format($nominal);	
	}
}
// END RiskIssue_model Class

/* End of file RiskIssue_model.php */
/* Location: ./application/models/RiskIssue_model.php */
?>